<template>
  <el-dialog
    v-el-drag-dialog
    width="400px"
    :visible="showDialog"
    :title="title"
    custom-class="modal-form"
    :show-close="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="onFormClosed"
  >
    <el-form
      ref="formOrganizationUnit"
      :model="organizationUnit"
    >
      <el-form-item
        prop="displayName"
        label="机构名称"
        :rules="{
          required: true,
          message: '请输入组织机构名称',
          trigger: 'blur'
        }"
      >
        <el-input
          v-model="organizationUnit.displayName"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          class="cancel"
          type="info"
          @click="onFormClosed"
        >
          取消
        </el-button>
        <el-button
          class="confirm"
          type="primary"
          icon="el-icon-check"
          @click="onSave"
        >
          确定
        </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { create, get, update } from '@/api/organizationunit'
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui

export default {
  name: 'CreateOrUpdateOrganizationUnit',
  components: { },
  directives: { elDragDialog },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    organizationUnitId: {
      type: String,
      default: ''
    },
    onOrganizationUnitChanged: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      organizationUnit: { },
      dialogFormVisible: false,
      dialogStatus: '',
      rules: {
        executionTime: [{ required: true, message: '必须填写', trigger: 'blur' }],
        executionDuration: [{ required: true, message: '必须填写', trigger: 'blur' }]
      },
      downloadLoading: false,
      loading: false,
      tempRoute: {},
      activeNames: ['1']
    }
  },
  watch: {
    showDialog() {
      this.handleGetOrganizationUnit()
    }
  },
  created() {
    this.handleGetOrganizationUnit()
  },
  methods: {
    handleGetOrganizationUnit() {
      if (this.showDialog && this.isEdit && this.organizationUnitId) {
        get(this.organizationUnitId).then(response => {
          this.organizationUnit = response.data
        })
      }
    },
    onSave() {
      const formOrganizationUnit = this.$refs.formOrganizationUnit
      formOrganizationUnit.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            update(this.organizationUnit.id, this.organizationUnit).then(response => {
              this.onOrganizationUnitChanged(response.data)
              this.onFormClosed()
            })
          } else {
            if (this.organizationUnitId) {
              this.organizationUnit.parentId = this.organizationUnitId
            }
            create(this.organizationUnit).then(response => {
              this.onOrganizationUnitChanged(response.data)
              this.onFormClosed()
            })
          }
        }
      })
    },

    onFormClosed() {
      const formOrganizationUnit = this.$refs.formOrganizationUnit
      formOrganizationUnit.resetFields()
      this.$emit('closed')
    }
  }
}
</script>

<style scoped>
.confirm {
  position: absolute;
  right: 10px;
  width:100px;
}
.cancel {
  position: absolute;
  right: 120px;
  width:100px;
}
</style>
